Main{-silly.hs-}

Plain source file: silly.hs (2005-02-11)

This is a small example illustrating how to create a simple command line interface using command-line parsing combinators.

import CmdLineParser4
import MUtils((@@),ifM)
import Directory
import Time

main = run silly

The command line syntax:

silly
  = cmd "cat"  cat  <@ files              --: "concatenate files"
  ! cmd "ls"   ls   <@ flag "-l" <@ files --: "list files (long)"
  ! cmd "date" date			  --: "print current date and time"
  ! cmd "help" help		          --: "show usage"

A file name is a non-empty argument that doesn't start with a dash.

files = many file
file = token check "<filename>"
  where
    check "" = Nothing
    check ('-':_) = Nothing
    check path = Just path

Implementation of the functionality. Notice in particular how easy it is to implement the help command.

help = putStrLn (usage "silly" silly)

date = print =<< getClockTime

cat = mapM_ (putStr @@ readFile)

ls True _ = fail "ls -l not implemented yet"
ls False paths = mapM_ ls1 paths

ls1 path = ifM (doesDirectoryExist path)
	       (putStr . unlines =<< getDirectoryContents path)
	       (ifM (doesFileExist path)
		    (putStrLn path)
		    (fail $ "No such file or directory: "++path))

Index

(HTML for this module was generated on 2005-02-11. About the conversion tool.)